package com.cskt.controller;

import com.cskt.pojo.User;
import com.cskt.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by 曾小丽 on 2018/3/27.
 */

@Controller
@RequestMapping("smbms")
public class SController {

    @Autowired
    private userService user;
    @RequestMapping("ck")
    public  String ChaXun(HttpServletRequest request, Model model){
        System.out.println("总数为："+user.zs());
        String name=request.getParameter("name");
        String indexPage=request.getParameter("indexPage");
        if(indexPage==null){
            indexPage="1";
        }
        Integer index1=Integer.parseInt(indexPage);
        System.out.println("第"+index1+"页");
        Integer Count=5;
        int AllCount=0;
        int zs=user.zs();
        if(zs%Count==0){
            AllCount=zs/Count;
        }else{
            AllCount=zs/Count+1;
        }
        model.addAttribute("index",index1);
        System.out.println("查询");
        List<User> list=null;
        if (name!=""&&name!=null){
            list=user.getAllInfoByName(name,index1,Count);
        }else{
            list=user.getAllInfo(null,index1,Count);
        }
        model.addAttribute("list",list);
        model.addAttribute("all",AllCount);
        return  "show";
    }

    @RequestMapping("insert")
    public  String insert(){
        return  "Add";
    }

    @RequestMapping("add")
    public  String Add(HttpServletRequest request){
       /* User u=new User();
        String uName=request.getParameter("uName");
        String uPwd=request.getParameter("uPwd");
        String uCode=request.getParameter("uCode");
        System.out.println(uName);   //sout
        u.setUserName(uName);
        u.setUserPassword(uPwd);
        u.setUserCode(uCode);
        Integer info=user.Add(u);
        if(info>0){
            System.out.println("┗|｀O′|┛ 嗷~~新增succes");
        }else{
            System.out.println("┗|｀O′|┛ 嗷~~新增失败");
            return "redirect:ck";
        }*/
       User user1=new User();
        String uName=request.getParameter("userName");
        String uCode=request.getParameter("userCode");
        String birthday=request.getParameter("birthday");
        user1.setUserCode(uCode);
        user1.setUserName(uName);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date time=sdf.parse(birthday);
            user1.setBirthday(time);
            user.Add(user1);
            Integer info=user.Add(user1);
            if(info>0){
                System.out.println("┗|｀O′|┛ 嗷~~新增succes");
            }else{
                System.out.println("┗|｀O′|┛ 嗷~~新增失败");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "redirect:ck";
    }


    @RequestMapping("del")
    public  String Del(HttpServletRequest request){
        String uid=request.getParameter("uid");
        System.out.println(uid);
        Integer info= user.Del(uid);
        if(info>0){
            System.out.println("┗|｀O′|┛ 嗷~~删除succes");
        }else{
            System.out.println("┗|｀O′|┛ 嗷~~删除失败");
        }
        return "redirect:ck";
    }

    @RequestMapping("xiu")
    public  String xiu(HttpServletRequest request, Model model){
        String uName=request.getParameter("uName");
        model.addAttribute("uName",uName);
        String uid=request.getParameter("uid");
        model.addAttribute("uName",uName);
        model.addAttribute("uid",uid);
        return  "Update";
    }

    @RequestMapping("update")
    public  String Update(HttpServletRequest request){
        String uCode=request.getParameter("uCode");
        String uPwd=request.getParameter("uPwd");
        String uid=request.getParameter("uid");
        User u=new User();
        u.setUserCode(uCode);
        u.setUserPassword(uPwd);
        u.setId(Integer.parseInt(uid));
        Integer info=user.Update(u);
        if(info>0){
            System.out.println("┗|｀O′|┛ 嗷~~修改succes");
        }else{
            System.out.println("┗|｀O′|┛ 嗷~~修改失败");
        }
        return "redirect:ck";
    }

    @RequestMapping("fm")
    public  String userAdd(@ModelAttribute("userAdd")User user){
        return "Add";
    }
}
